package com.Controller.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sql.DAO;

import com.Model.Date.DateTime;
import com.Model.Pojo.Users;

public class ZhuCe extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
    	response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
    	HttpSession session = request.getSession();
    	Random rand = new Random();
    	int zh = rand.nextInt(90000) + 10001;
    	String zhanghao = zh + "";
		String name = request.getParameter("name");
		String email = request.getParameter("Email");
		String shoujihao = request.getParameter("shoujihao");
		String pass1 = request.getParameter("password");
		String pass2 = request.getParameter("reenterpassword");
		int yue = 100;
		int money = 0;
		Users user = new Users();
		pass1 = pass1 + "1";
		String sql = "insert into users values('" + zhanghao + "','" 
			+ pass1 + "','" + yue + "" + "','" + name + "','" 
			+ email + "','" + shoujihao + "','" + money + "')";
		DAO dao = new DAO();
		dao.zengjia(sql);
		user.setZhanghao(zhanghao);
		user.setMima(pass1);
		user.setYue(yue);
		user.setXingming(name);
		user.setYouxiang(email);
		user.setShoujihao(shoujihao);
		user.setMoney(money);
		session.setAttribute("users", user);
		
		DateTime date = new DateTime();
		sql = "insert into cunkuanqvkuan (name,money,style,date,yue)value('"
			+ zhanghao + "','100','1','" + date.getSettime() + "','100')";
		dao.zengjia(sql);
		
		String usersname = zhanghao;
		String style1 = "存款";
		int cunmoney = 100;
		int cash = 0;
		String source = "开户预存";
		
		sql = "insert into moneylist (name,style,value,date,cash,source)value('" + usersname + "','" + style1
		+ "','" + cunmoney + "','" + date.getSettime() + "','" + cash + "','" + source + "')";
		dao.zengjia(sql);
		
		// 为JSP实例化一个请求分派器。
		response.sendRedirect("/Bank/WebPage/FrontPage/RegisterSuccessPage/Register.jsp");
		out.flush();
		out.close();
	}
}
